package com.android.example.asynctasktest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MyTest extends Activity{

	private Button mButton;
	private ImageView mImageView;
	private String url = "http://c.hiphotos.baidu.com/image/pic/item/63d0f703918fa0ec4a43ba4c249759ee3c6ddbef.jpg";
	private ProgressDialog  mProgressDialog ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mytest);
		mButton = (Button)this.findViewById(R.id.button1);
		mImageView = (ImageView)this.findViewById(R.id.imageView1);
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setTitle("提示信息");
		mProgressDialog.setMessage("请稍后");
		mProgressDialog.setCancelable(false);
		mButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new MyTask().execute(url);
			}
		});
	}
	
	public class MyTask extends  AsyncTask<String, Integer, Bitmap>{
		

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			mProgressDialog.show();
		}

		

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			mProgressDialog.setProgress(values[0]);
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			// TODO Auto-generated method stub
			Bitmap mBitmap = null;
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			InputStream inputStream = null;
			
			try {
				HttpClient mHttpClient = new DefaultHttpClient();
				HttpGet mHttpGet = new HttpGet(params[0]);
				HttpResponse httpResponse = mHttpClient.execute(mHttpGet);
				if (httpResponse.getStatusLine().getStatusCode()==200){
					inputStream = httpResponse.getEntity().getContent();
					//首先或者文件的总长度
					long file_len = httpResponse.getEntity().getContentLength();
					int len = 0;
					byte[] data = new byte[1024];
					int total_len = 0;
					while((len=inputStream.read(data))!=-1){
						total_len+=len;
						int value = (int)((total_len/(float)file_len)*100);
						publishProgress(value);
						outStream.write(data,0,len);
					}
					byte[] result = outStream.toByteArray();
					mBitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
				}

			}catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(inputStream!=null){
					try {
						inputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				}
			}
			
			return mBitmap;
		}
		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			mProgressDialog.dismiss();
			mImageView.setImageBitmap(result);
		}
		
	}

}
